package com.zky.basic.controller;

import com.zky.basic.dto.PhoneDto;
import com.zky.basic.exception.BusinessException;
import com.zky.basic.service.IVerifyCodeService;
import com.zky.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private IVerifyCodeService verifyCodeService;

    @GetMapping("/image/{key}")
    public AjaxResult getImageCode(@PathVariable("key") String key){
        try {
            String base64Str = verifyCodeService.getImageCode(key);
            return AjaxResult.me().setObj(base64Str);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙");
        }
    }


    @PostMapping("/phone")
    public AjaxResult getPhoneCode(@RequestBody PhoneDto phoneDto){
        try {
            verifyCodeService.getPhoneCode(phoneDto);
            return  new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙");
        }
    }

    @PostMapping("/binderSmsCode")
    public AjaxResult binderSmsCode(@RequestBody PhoneDto phoneDto){
        try {
            verifyCodeService.binderSmsCode(phoneDto);
            return  new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙");
        }
    }


}
